﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using Newtonsoft.Json;
using System.Web.Services;
using System.Web.Script.Services;

namespace PromotionEngine
{
    public partial class ReportUserDefined : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        public static string GetReportParameter(string reportID)
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReportParameterByReportID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), reportID);
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    if (r["ParaFromQuery"].ToString().Trim().Length > 0)
                    {
                        r["ReferenceName"] = "Query";
                    }
                }
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        public static string LoadReport(string oParams, string reportID)
        {
            string query = string.Empty;
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.LoadUserDefinedReport(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), reportID, oParams);
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string LoadReportParaFromQuery(string reportDetailID)
        {
            string query = string.Empty;
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.LoadReportParameterFromQuery(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), reportDetailID);
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
    }
}